const handlePermissionDenied = async (fn: () => void) => {
  const checkPermissionSupport = 'permissions' in navigator
  const checkMediaDevicesSupport = async () => {
    if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
      try {
        // 使用getUserMedia方法获取麦克风权限
        const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
        // 录音权限已授权，去释放资源
        stream.getTracks().forEach((track) => track.stop());
        return true
      } catch (error) {
        console.error("navigator getUserMedia failed:", error);
        return false
      }
    } else {
      return false
    }

  }

  const permissionDeniedMessage = "您已拒绝麦克风权限。请在设置中授权麦克风权限，以便使用录音功能。";
  const permissionPromptMessage = "浏览器将提示您授权麦克风权限。请在设置中授权麦克风权限，以便使用录音功能。";
  const permissionQueryFailedMessage = "无法查询麦克风权限状态。请在设置中授权麦克风权限，以便使用录音功能。";
  const deviceNotSupportedMessage = "您的设备不支持录音功能。建议您使用支持该功能的浏览器或更新系统版本";

  if (checkPermissionSupport) {
    console.log("navigator.permissions is supported");
    try {
      const permissionStatus = await navigator.permissions.query({ name: "microphone" as PermissionName });
      console.log('permissionStatus', permissionStatus);
      if (permissionStatus.state === "granted") {
        // 录音权限已授权
        fn();
      } else if (permissionStatus.state === "prompt") {
        // 浏览器将提示用户进行授权，部分浏览器没有提示，需直接请求媒体设备
        const mediaDevicesPermission = await checkMediaDevicesSupport();
        if (mediaDevicesPermission) {
          // 录音权限已授权
          fn();
        } else {
          alert(permissionPromptMessage);
        }
      } else if (permissionStatus.state === "denied") {
        // 录音权限被拒绝
        alert(permissionDeniedMessage);
      }
    } catch (error) {
      console.error("Permission query failed:", error);
      alert(permissionQueryFailedMessage);
    }
  } else if (await checkMediaDevicesSupport()) {
    console.log("navigator.permissions is not supported, but navigator.mediaDevices.getUserMedia is supported");
    fn();
  } else {
    alert(deviceNotSupportedMessage);
  }
};